package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.PluginParamTemplate;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 插件参数模板类-[表名: 插件参数模板(plugin_param_template)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class PluginParamTemplateDao {

	/* 连接对象 */
	Connection conn = null;	

	public PluginParamTemplateDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertPluginParamTemplate(PluginParamTemplate pluginParamTemplate) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into plugin_param_template ( "
                + "param_id,plugin_id,param_category,param_code,param_name,param_html_tag,param_html_option,param_orderid,parent_param_id"
                + ") values ("+ "?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, pluginParamTemplate.getParamId());
            ps.setObject(2, pluginParamTemplate.getPluginId());
            ps.setObject(3, pluginParamTemplate.getParamCategory());
            ps.setObject(4, pluginParamTemplate.getParamCode());
            ps.setObject(5, pluginParamTemplate.getParamName());
            ps.setObject(6, pluginParamTemplate.getParamHtmlTag());
            ps.setObject(7, pluginParamTemplate.getParamHtmlOption());
            ps.setObject(8, pluginParamTemplate.getParamOrderid());
            ps.setObject(9, pluginParamTemplate.getParentParamId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert PluginParamTemplate is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(PluginParamTemplate pluginParamTemplate) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from plugin_param_template where 1=1"
                + " AND param_id = ? "
                );
            ps.setObject(1, pluginParamTemplate.getParamId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete PluginParamTemplate is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(PluginParamTemplate pluginParamTemplate) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update plugin_param_template set "
                + " param_id = ? ,"
                + " plugin_id = ? ,"
                + " param_category = ? ,"
                + " param_code = ? ,"
                + " param_name = ? ,"
                + " param_html_tag = ? ,"
                + " param_html_option = ? ,"
                + " param_orderid = ? ,"
                + " parent_param_id = ? "
                + " where 1=1"
                + " AND param_id = ? "
                );
            ps.setObject(1, pluginParamTemplate.getParamId());
            ps.setObject(2, pluginParamTemplate.getPluginId());
            ps.setObject(3, pluginParamTemplate.getParamCategory());
            ps.setObject(4, pluginParamTemplate.getParamCode());
            ps.setObject(5, pluginParamTemplate.getParamName());
            ps.setObject(6, pluginParamTemplate.getParamHtmlTag());
            ps.setObject(7, pluginParamTemplate.getParamHtmlOption());
            ps.setObject(8, pluginParamTemplate.getParamOrderid());
            ps.setObject(9, pluginParamTemplate.getParentParamId());
            ps.setObject(10, pluginParamTemplate.getParamId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update PluginParamTemplate is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public PluginParamTemplate queryByPk(PluginParamTemplate pluginParamTemplate) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        PluginParamTemplate result = null;
        try {
            ps = conn.prepareStatement("select param_id,plugin_id,param_category,param_code,param_name,param_html_tag,param_html_option,param_orderid,parent_param_idfrom plugin_param_template where 1=1 "
                + " AND param_id = ? "
                );
            ps.setObject(1, pluginParamTemplate.getParamId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new PluginParamTemplate();
				result.setParamId(rs.getString("param_id"));
				result.setParamCategory(rs.getString("param_category"));
				result.setParamCode(rs.getString("param_code"));
				result.setParamName(rs.getString("param_name"));
				result.setParamHtmlTag(rs.getString("param_html_tag"));
				result.setParamHtmlOption(rs.getString("param_html_option"));
				result.setParentParamId(rs.getString("parent_param_id"));
			}
        } catch (SQLException e) {
            throw new SQLException("update PluginParamTemplate is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 pluginParamTemplate 参数列表查询 不分页
	* @return int
	*/
    public List<PluginParamTemplate> queryAll(PluginParamTemplate pluginParamTemplate) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PluginParamTemplate> result = new ArrayList<>();
        try {
            String sql = "select param_id,plugin_id,param_category,param_code,param_name,param_html_tag,param_html_option,param_orderid,parent_param_idfrom plugin_param_template where 1=1 ";
                if (pluginParamTemplate.getParamId() != null) {
                    sql = sql + " AND param_id =  '" + pluginParamTemplate.getParamId() + "'";
                }
                if (pluginParamTemplate.getPluginId() != null) {
                    sql = sql + " AND plugin_id =  '" + pluginParamTemplate.getPluginId() + "'";
                }
                if (pluginParamTemplate.getParamCategory() != null) {
                    sql = sql + " AND param_category =  '" + pluginParamTemplate.getParamCategory() + "'";
                }
                if (pluginParamTemplate.getParamCode() != null) {
                    sql = sql + " AND param_code =  '" + pluginParamTemplate.getParamCode() + "'";
                }
                if (pluginParamTemplate.getParamName() != null) {
                    sql = sql + " AND param_name =  '" + pluginParamTemplate.getParamName() + "'";
                }
                if (pluginParamTemplate.getParamHtmlTag() != null) {
                    sql = sql + " AND param_html_tag =  '" + pluginParamTemplate.getParamHtmlTag() + "'";
                }
                if (pluginParamTemplate.getParamHtmlOption() != null) {
                    sql = sql + " AND param_html_option =  '" + pluginParamTemplate.getParamHtmlOption() + "'";
                }
                if (pluginParamTemplate.getParamOrderid() != null) {
                    sql = sql + " AND param_orderid =  '" + pluginParamTemplate.getParamOrderid() + "'";
                }
                if (pluginParamTemplate.getParentParamId() != null) {
                    sql = sql + " AND parent_param_id =  '" + pluginParamTemplate.getParentParamId() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				PluginParamTemplate obj = new PluginParamTemplate();
				obj.setParamId(rs.getString("param_id"));
				obj.setParamCategory(rs.getString("param_category"));
				obj.setParamCode(rs.getString("param_code"));
				obj.setParamName(rs.getString("param_name"));
				obj.setParamHtmlTag(rs.getString("param_html_tag"));
				obj.setParamHtmlOption(rs.getString("param_html_option"));
				obj.setParentParamId(rs.getString("parent_param_id"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update PluginParamTemplate is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
